package com.tianqu.tms.www.finance.controller;


import java.text.SimpleDateFormat;
import java.util.Date;







import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;

import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.transferserviceI.TransferDataSyn50000Bo;
import com.tianqu.tms.entity.IntegralRegister;
import com.tianqu.tms.entity.Member;

import com.tianqu.tms.www.finance.viewhelper.PropertyDepositViewHelper;

public class PropertyDepositInputResultController extends TmsController {
	private Logger logger = Logger.getLogger(CarProxyRetreatTransAccInputController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		  long startTime=System.currentTimeMillis();
		String fees1 = "";
		String fees2 = "";
		TxController controller = TxController.getTxController();
		try {
			PropertyDepositViewHelper view = (PropertyDepositViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			logger.info("property deposit,info:memberId:"+view.getMemberId()+" price:"+view.getPrice()+" type:"+view.getType()+" remark:"+view.getRemark());
			logger.info("测试短信信息 以及内容");
			TmsUserProfile profile = uc.getUserProfile();
			Member member = Member.getInstance();
			member = member.findMemberInfoByMemId(view.getMemberId());
			//保证金 加款与扣款
			int margin = 0;
			if("1".equals(view.getType())){ margin = Integer.parseInt(member.getMargin()) + Integer.parseInt(view.getPrice()); }
			else if("2".equals(view.getType())){ margin = Integer.parseInt(member.getMargin()) - Integer.parseInt(view.getPrice()); }
			//数据放到  bean中进行存储
			member.setMargin(margin+"");
			member.setAucMargin(member.getAucMargin());
			member.setVirtualMargin(StringUtil.removeDH(member.getVirtualMargin()));
			member.setMemId(Integer.parseInt(view.getMemberId()));
			member.setUpdateUserId(profile.getUniqueId());
			member.updateAccountInfoByMemId(member);
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
			String insertTime = dateFormat.format(now);
			//完善登记信息吗？？？？？
			IntegralRegister register = new IntegralRegister();
			register.setMemberId(view.getMemberId());
			register.setMemberType(1);
			register.setSerial("");
			register.setLicensePlate("");
			register.setTransferPrice(Integer.parseInt(view.getPrice()));
			//从视图中得到标记值  1 物业保证金缴费 2 物业保证金扣罚			
			String remark = view.getRemark()==null?"":view.getRemark();
			if("1".equals(view.getType())){
				register.setRemark(remark+"物业保证金缴费"+view.getPrice());
				register.setPlusMinus(1);
				register.setChargeType(8);
				//动态拼缴费与扣费
				fees1="[物业保证金缴费通知]";
				fees2="缴费金额为:";
			}else if("2".equals(view.getType())){
				register.setRemark(remark+"物业保证金扣罚"+view.getPrice());
				register.setPlusMinus(2);
				register.setChargeType(9);
				fees1="[物业保证金扣罚通知]";
				fees2="扣罚金额为";
			}

			register.setInsertUserId(profile.getUniqueId());
			register.setIntegral(0);
			register.insertRegisterInfo(register);			
			controller.commit();
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			
			
			//
			MobileMessage message = new MobileMessage();
			message.putValue("memberId",view.getMemberId());
			message.setMessage("物业保证金 - 缴费/扣罚接口失败,会员ID为#memberId#");
			TransferDataSyn50000Bo api = new TransferDataSyn50000Bo();
			api.setMobileMessage(message);
			api.setMarginType("1");
			api.setMemberId(view.getMemberId());
			api.setPrice(StringUtil.removeDH(view.getPrice()));
			api.setRemark(view.getRemark());
			api.setTreatment(view.getType());
			if("2".equalsIgnoreCase(view.getDxtzType())) api.setWeixinMessage(view.getContactPhone());
			PullingExecutor.getInstance().submit(api);
		} catch (Exception e) {
			logger.error(e);
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
		long endTime=System.currentTimeMillis();

	    float excTime=(float)(endTime-startTime)/1000;

	    System.out.println("@@@@@@@@@@@execute(UserContext uc)执行时间："+excTime+"s");
	}

}
